package com.dyzx.hbb.modules.sys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dyzx.hbb.common.base.PageResult;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.modules.sms.service.SmsService;
import com.dyzx.hbb.modules.sys.entity.SysUser;
import com.dyzx.hbb.modules.sys.query.UserQuery;
import com.dyzx.hbb.modules.sys.service.SysUserService;
import com.dyzx.hbb.modules.sys.mapper.SysUserMapper;
import com.dyzx.hbb.security.model.SysUserInfo;
import com.dyzx.hbb.aliyun.sms.utils.SmsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【sys_user(用户表)】的数据库操作Service实现
 * @createDate 2025-04-14 13:29:08
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
        implements SysUserService {
    @Autowired
    private SmsClient smsClient;
    @Autowired
    private SmsService smsService;

    @Override
    public SysUser getByAccount(String username) {
        if (username != null) {
            return baseMapper.selectOne(
                    new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<SysUser>()
                            .eq("account", username).last("limit 1")
            );
        }
        return null;
    }
    @Cacheable(value = "user:menu:permissions:userId", key = "#userId")
    @Override
    public List<String> getMenuPermissions(Long userId) {
        if (userId != null) {
            return baseMapper.getMenuPermissions(userId);
        }

        return List.of();
    }

    @Override
    public PageResult<SysUser> findByPage(UserQuery query) {

        // 创建分页对象
        Page<SysUser> page = new Page<>(1, 10);

        // 构建查询条件
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(query.getAccount()), SysUser::getAccount, query.getAccount())
                .like(StringUtils.isNotBlank(query.getBroker()), SysUser::getName, query.getBroker());

        // 执行分页查询
        Page<SysUser> data = this.page(page, wrapper);


        return PageResult.parsePage(data);
    }

    @Override
    public boolean validateSmsCode(String phone, String smsCode) {
        if (phone != null && smsCode != null) {
        return     smsService.validateSmsCode(phone, smsCode);
        }
        return false;
    }

    @Override
    public void sendNewPassword(String phone, String newPassword) {
        if (phone != null) {
            // 发送短信
            // ...
                 smsService.sendNewPassword(phone, newPassword);
        }

    }


    @Override
    public SysUser getByPhone(String phone) {
        if (phone != null) {
            return baseMapper.selectOne(
                    new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<SysUser>()
                            .eq("phone", phone).eq("deleted", 0).eq("status", 1).last("limit 1")
            );
        }
        return null;
    }

    @Override
    public SysUserInfo getLoginUserInfo(Long userId) {
        MyLambdaQueryWrapper<SysUser> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.organName(SysUser::getOrganId).as("organ_name").deptName(SysUser::getDeptId).as("dept_name").select(SysUser::getName, SysUser::getType, SysUser::getPosition, SysUser::getBroker, SysUser::getDeptId, SysUser::getBroker);
        wrapper.imageUrl(SysUser::getAvatar).as("avatar_url");
        wrapper.eq(SysUser::getId, userId).last("limit 1");
        Map<String, Object> map = this.getMap(wrapper);
        if (map != null) {
            SysUserInfo userInfo = new SysUserInfo();
          //  BeanUtils.copyProperties(map, userInfo);
            userInfo.setName((String) map.get("name"));
            userInfo.setType((Integer) map.get("type"));
            userInfo.setOperator((Integer) map.get("operator"));
            userInfo.setBroker((String) map.get("broker"));
            userInfo.setDeptId((Long) map.get("dept_id"));
            userInfo.setDeptName((String) map.get("dept_name"));
            userInfo.setOrganName((String) map.get("organ_name"));
            userInfo.setBroker((String) map.get("broker"));
            userInfo.setAvatarUrl((String) map.get("avatar_url"));

            return userInfo;
        }

        return null;
    }

    @Override
    public List<Long> findByOrganIds(List<String> list) {
        MyLambdaQueryWrapper<SysUser> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.in(SysUser::getOrganId, list);
        return list(wrapper).stream().map(SysUser::getId).collect(Collectors.toList());
    }

    @Override
    public List<Long> findByDeptIds(List<String> list) {
        MyLambdaQueryWrapper<SysUser> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.in(SysUser::getDeptId, list);
        return list(wrapper).stream().map(SysUser::getId).collect(Collectors.toList());
    }
}




